Skip to content

Conversation

@recursingfeynman
Copy link

@recursingfeynman recursingfeynman commented Oct 17, 2025

When Langfuse is enabled, the object passed to OpenAI’s stream manager as raw_stream is adapter LangfuseResponseGeneratorAsync. The OpenAI manager sets self._response = raw_stream.response and then calls await self._response.aclose(). LangfuseResponseGeneratorAsync previously set response to the AsyncStream itself (not to httpx.Response), so the manager tried to call aclose() on an AsyncStream, causing:

AttributeError: 'AsyncStream' object has no attribute 'aclose'

Steps to reproduce

  • openai == 2.4.0
  • langfuse == 3.7.0
from langfuse.openai import AsyncOpenAI

request_options = {
    "model": "...",
    "messages": [{"role": "user", "content": "Hello"}],
    "n": 1,
}

async with AsyncOpenAI().chat.completions.stream(**request_options) as stream:
    async for event in stream:
        print(event)

Important

Ensure raw_stream.response is httpx.Response by renaming response to stream and setting self.response to stream.response in relevant classes and functions.

  • Behavior:
    • Ensure raw_stream.response is httpx.Response in async adapter by renaming response to stream in _wrap and _wrap_async functions.
    • Set self.response to stream.response in LangfuseResponseGeneratorSync and LangfuseResponseGeneratorAsync classes.
  • Classes:
    • LangfuseResponseGeneratorSync: Renamed response to stream and set self.response to stream.response.
    • LangfuseResponseGeneratorAsync: Renamed response to stream and set self.response to stream.response.
  • Functions:
    • _wrap: Renamed response to stream.
    • _wrap_async: Renamed response to stream.

This description was created by Ellipsis for 816c68a. You can customize this summary. It will automatically update as commits are pushed.

@CLAassistant
Copy link

CLAassistant commented Oct 17, 2025

CLA assistant check
All committers have signed the CLA.

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Oct 17, 2025

Greptile encountered an error while reviewing this PR. Please reach out to [email protected] for assistance.

@recursingfeynman recursingfeynman changed the title fix: ensure raw_stream.response is httpx.Response in async adapter fix: ensure raw_stream.response is httpx.Response in LangfuseResponseGeneratorAsync Oct 17, 2025
@recursingfeynman recursingfeynman changed the title fix: ensure raw_stream.response is httpx.Response in LangfuseResponseGeneratorAsync fix: ensure raw_stream.response is httpx.Response in langfuse streaming adapter Oct 17, 2025
@hassiebp hassiebp self-requested a review October 20, 2025 11:37
@recursingfeynman
Copy link
Author

@hassiebp Hi! Maybe add some tests?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants